package com.fenghongzhang.day007;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;

import androidx.appcompat.app.AppCompatActivity;

import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText editText;
    private Button send;
    private ProgressBar bar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         editText = findViewById(R.id.input);

         send = findViewById(R.id.send);
         send.setOnClickListener(this);

        bar = (ProgressBar) findViewById(R.id.bar);
        bar.setMax(3);
        bar.setProgress(3);
    }

    @Override
    public void onClick(View v) {
        final String string = editText.getText().toString();

        bar.setVisibility(View.VISIBLE);

        final Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                //进度减去1
                bar.incrementProgressBy(-1);

                //不能再子线程更新ui->主线程才能更新
                //所有这里的代码都会执行在主线程中.
//                runOnUiThread(new Runnable() {
//                    @Override
//                    public void run() {
//                        editText.setText("3");
//                    }
//                });

                if(bar.getProgress() == 0){ //够三秒了

                    //跳转
                    Intent intent = new Intent(MainActivity.this, Main2Activity.class);
                    //1,intent 直接传值
                    intent.putExtra("name",string);
                    startActivity(intent);
                    //取消定时器
                    timer.cancel();
                }
            }
        },0,1000);



    }
}
